﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Gin.Controls
{

    public abstract class UserInputBrowse: UserInputControl
    {
        private const string DATA_TAG = "DataControl";

        private const int TOTAL_CONTROL_HEIGHT = 50;
        private const int SUBCONTROL_PADDING = 5;
        private const int TEXTBOX_TOP = 20;
        private const int MIN_TEXTBOX_WIDTH = 40;
        private const int BUTTON_WIDTH = 50;
        private const string BUTTON_TEXT = "Обзор...";
        private const int BUTTON_HEIGHT = 20;

        public string Caption { get; set; }
        public string InitialValue { get; set; }

        protected TextBox textbox;

        public override Control Create(int containerWidth)
        {
            Height = TOTAL_CONTROL_HEIGHT;

            Panel control = new Panel();
            control.BorderStyle = BorderStyle.FixedSingle;
            control.Width = containerWidth;
            control.Height = TOTAL_CONTROL_HEIGHT;

            Label caption = new Label();
            caption.AutoSize = true;
            caption.Text = Caption;
            caption.Top = SUBCONTROL_PADDING;
            caption.Left = SUBCONTROL_PADDING;
            control.Controls.Add(caption);

            textbox = new TextBox();
            textbox.Text = InitialValue;
            textbox.Left = SUBCONTROL_PADDING;
            textbox.Top = TEXTBOX_TOP;
            textbox.Tag = DATA_TAG;
            textbox.Width = Math.Max(containerWidth - BUTTON_WIDTH - SUBCONTROL_PADDING * 3, MIN_TEXTBOX_WIDTH);
            control.Controls.Add(textbox);

            Button browseButton = new Button();
            browseButton.Text = BUTTON_TEXT;
            browseButton.Width = BUTTON_WIDTH;
            browseButton.Top = TEXTBOX_TOP;
            browseButton.Height = BUTTON_HEIGHT;
            browseButton.Left = containerWidth - BUTTON_WIDTH - SUBCONTROL_PADDING;
            browseButton.Click += new EventHandler(OnBrowseButtonClick);
            control.Controls.Add(browseButton);

            return control;
        }

        protected abstract void OnBrowseButtonClick(object sender, EventArgs e);

        protected string CurrentValue 
        {
            get
            {
                return textbox.Text;
            }
            set
            {
                textbox.Text = value;
            }
        }

        public override object ReadValue(Control control)
        {
            return ReadTextBoxDefault(control, DATA_TAG);
        }
    }
}
